package com.czk.zookeeper.zookeepercli.create;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.ACL;
import org.apache.zookeeper.data.Id;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;

/**
 * 节点的创建
 *
 * @Author:ChenZhangKun
 * @Date: 2021/1/3 20:37
 */
public class ZkCreateTest {
    ZooKeeper zooKeeper = null;

    /**
     * before上创建连接
     *
     * @throws IOException
     */
    @Before
    public void before() throws IOException {
        // 由于zookeeper连接是异步的，所以需要经主线程阻塞
        CountDownLatch countDownLatch = new CountDownLatch(1);
        zooKeeper = new ZooKeeper("192.168.25.133:2181", 10000, new Watcher() {
            @Override
            public void process(WatchedEvent watchedEvent) {
                // 连接成功
                if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {
                    System.out.println("连接创建成功");
                    // 释放主线程
                    countDownLatch.countDown();
                }
            }
        });
        // 阻塞主线程
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 关闭连接
     */
    @After
    public void after() {
        try {
            zooKeeper.close();
            System.out.println("关闭连接成功");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 单权限
     * 创建节点
     */
    @Test
    public void create() {
        // 创建节点
        try {
            // arg1 节点路径
            // arg2 节点数据
            // arg3 权限列表 ZooDefs.Ids.OPEN_ACL_UNSAFE 以world的方式进行授权
            // arg4 节点类型，持久化节点
            zooKeeper.create("/create/node1", "node1".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            System.out.println("节点创建成功");
        } catch (KeeperException | InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void create2() {
        // 创建节点
        try {
            List<ACL> acls = new ArrayList<>(10);
            // 授权模式和授权对象
            Id id = new Id("world", "anyone");
            // 权限设置
            acls.add(new ACL(ZooDefs.Perms.READ, id));
            acls.add(new ACL(ZooDefs.Perms.WRITE, id));
            // arg1 节点路径
            // arg2 节点数据
            // arg3 权限列表
            // arg4 节点类型，持久化节点
            zooKeeper.create("/create/node3", "node3".getBytes(), acls, CreateMode.PERSISTENT);
            System.out.println("节点创建成功");
        } catch (KeeperException | InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void create3() {
        Id id = new Id("ip", "192.168.25.134");
        List<ACL> acls = new ArrayList<>(10);
        acls.add(new ACL(ZooDefs.Perms.ALL, id));
        // 创建节点
        try {
            // arg1 节点路径
            // arg2 节点数据
            // arg3 权限列表 Z
            // arg4 节点类型，持久化节点
            zooKeeper.create("/create/node4", "node4".getBytes(), acls, CreateMode.PERSISTENT);
            System.out.println("节点创建成功");
        } catch (KeeperException | InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建有序节点
     */
    @Test
    public void create4() {
        // 创建节点
        try {
            // arg1 节点路径
            // arg2 节点数据
            // arg3 权限列表 ZooDefs.Ids.OPEN_ACL_UNSAFE 以world的方式进行授权
            // arg4 节点类型，持久化节点
            String result = zooKeeper.create("/create/node5", "node5".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
            System.out.println(result);
            System.out.println("节点创建成功");
        } catch (KeeperException | InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 异步方式创建节点
     */
    @Test
    public void create5() {
        // 创建节点
        try {
            // arg1 节点路径
            // arg2 节点数据
            // arg3 权限列表 ZooDefs.Ids.OPEN_ACL_UNSAFE 以world的方式进行授权
            // arg4 节点类型，持久化节点
            zooKeeper.create("/create/node6", "node6".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, new AsyncCallback.StringCallback() {
                @Override
                public void processResult(int i, String s, Object o, String s1) {
                    // 0代表成功
                    System.out.println(i);
                    // s 表示节点的路径
                    System.out.println(s);
                    // s1表示节点路径
                    System.out.println(s1);
                    // 0 上下文参数
                    System.out.println(o);
                }
            }, "I am context"); // ctx 上下文参数
            System.out.println("节点创建成功");
            Thread.sleep(10000);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
